package org.mybatis.generator.codegen.mybatis3.javamapper.elements.suffix;

import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.codegen.mybatis3.SuffixUtil;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Set;
import java.util.TreeSet;

/*********************************************************
 * 文件名称：SuffixDeleteByExampleMethodGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.javamapper.elements.suffix
 * 功能说明：带表后缀的条件删除方法生成器
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/28 15:18
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixDeleteByExampleMethodGenerator  extends AbstractJavaMapperMethodGenerator {


    @Override
    public void addInterfaceElements(Interface interfaze) {
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
        FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
        importedTypes.add(type);

        Method method = new Method(SuffixUtil.getSuffixMethodName(introspectedTable.getDeleteByExampleStatementId(),introspectedTable));
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setAbstract(true);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));
        method.addParameter(new Parameter(type, "example","@Param(\"example\")")); //$NON-NLS-1$

        SuffixUtil.addSuffixParam(importedTypes,method);

        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);

        addMapperAnnotations(method);

        if (context.getPlugins().clientDeleteByExampleMethodGenerated(method, interfaze, introspectedTable)
                && SuffixUtil.isEnableSuffix(introspectedTable)) {
            addExtraImports(interfaze);
            interfaze.addImportedTypes(importedTypes);
            // 删除原方法
            SuffixUtil.deleteOldMethod(interfaze, method);
            interfaze.addMethod(method);
        }
    }

    public void addMapperAnnotations(Method method) {
        // extension point for subclasses
    }

    public void addExtraImports(Interface interfaze) {
        // extension point for subclasses
    }
}
